import { __private, _decorator, Component, EventKeyboard, EventMouse, EventTarget, Input, input, KeyCode, math, Node, UITransform, v2, Vec2, Vec3 } from 'cc';
import { StateDefine } from '../Config';
import { PlayerIdleState } from './PlayerState/PlayerIdleState';
import { PlayerMoveState } from './PlayerState/PlayerMoveState';
import { PlayerAirState } from './PlayerState/PlayerAirState';
import { PlayerJumpState } from './PlayerState/PlayerJumpState';
import { Player } from './Player';
import { PlayerDashState } from './PlayerState/PlayerDashState';
import { PlayerWallSildeState } from './PlayerState/PlayerWallSildeState';
import { PlayerWallJump } from './PlayerState/PlayerWallJump';
import { PlayerAttackState } from './PlayerState/PlayerAttackState';
import { KeyboardInput } from '../../Input/KeyboardInput';
import { PlayerCounterAttackState } from './PlayerState/PlayerCounterAttackState';
import { PlayerAimSwordState } from './PlayerState/PlayerAimSwordState';
import { PlayerCatchSwordState } from './PlayerState/PlayerCatchSwordState';
import { PlayerAimThrowState } from './PlayerState/PlayerAimThrowState';
import { PlayerManager } from './PlayerManager';
const { ccclass, property } = _decorator;

@ccclass('PlayerControl')
export class PlayerControl extends Component {

    player: Player | null = null;
    inputKey:KeyboardInput =null;

    
    protected onEnable(): void {
        this.inputKey = new KeyboardInput(KeyCode.KEY_W, KeyCode.KEY_S, KeyCode.KEY_A, KeyCode.KEY_D)
    
    }

    start() {
        this.player = this.getComponent(Player);
        this.player.stateMgr.registState(new PlayerIdleState(StateDefine.Idle, this.player, StateDefine.Idle));
        this.player.stateMgr.registState(new PlayerMoveState(StateDefine.Run, this.player,  StateDefine.Run));
        this.player.stateMgr.registState(new PlayerAirState(StateDefine.Fall, this.player,  StateDefine.Fall));
        this.player.stateMgr.registState(new PlayerJumpState(StateDefine.Jump, this.player, StateDefine.Jump));
        this.player.stateMgr.registState(new PlayerDashState(StateDefine.Dash, this.player, StateDefine.Dash));
        this.player.stateMgr.registState(new PlayerWallSildeState(StateDefine.WallSilde, this.player,   StateDefine.WallSilde));
        this.player.stateMgr.registState(new PlayerWallJump(StateDefine.WallJump, this.player,  StateDefine.Jump));
        
        this.player.stateMgr.registState(new PlayerAttackState(StateDefine.Attack, this.player, StateDefine.Attack));
        this.player.stateMgr.registState(new PlayerCounterAttackState(StateDefine.Counter, this.player, StateDefine.Counter));
        
        this.player.stateMgr.registState(new PlayerAimSwordState(StateDefine.AimSword,this.player,StateDefine.AimSword));
        this.player.stateMgr.registState(new PlayerCatchSwordState(StateDefine.CatchSword,this.player,StateDefine.CatchSword));
        this.player.stateMgr.registState(new PlayerAimThrowState(StateDefine.ThrowSword,this.player,StateDefine.ThrowSword))
        
        this.player.stateMgr.startWith(StateDefine.Idle);

        input.on(Input.EventType.MOUSE_DOWN,this.mouseDown,this);
        input.on(Input.EventType.MOUSE_UP,this.mouseUp,this);
        input.on(Input.EventType.MOUSE_MOVE,this.mouseMove,this);
    }
    mouseUp(event:EventMouse) {
        this.player.mouseInput[event.getButton()] = false;
    }
    mouseDown(event:EventMouse) {
        this.player.mouseInput[event.getButton()] = true;
    }
    mouseMove(event:EventMouse) {
        const eventPos = new Vec3(event.getUILocation().x,event.getUILocation().y,0)
        // console.info("1"+eventPos);
        // const worldPos = this.player.node.getComponent(UITransform).convertToNodeSpaceAR(eventPos);
        // console.info("2"+worldPos);
        // worldPos.subtract(this.player.node.getWorldPosition());
        // console.info("3"+worldPos);
        this.player.mousePos = eventPos;
    }
    protected update(dt: number): void {
        
        this.player.input = this.inputKey.getAxis();
        this.player.pressedKeys = this.inputKey.getPressedKeys();
        
        // console.info(this.player.input)
    }
}


